<?php
 
  class Device_m extends CI_Model
  {
      
      function __construct()
      {
         parent::__construct();
         $this->load->database();
     }
        
     function insert($arr)
     {
         $this->db->insert('device',$arr);
     }  
     
     function update($where,$arr)
     {  
         $this->db->where($where);
         $data = $this->db->update('device',$arr);
         return  $data;
     }
            
     function delete($where)
     {
         $this->db->where($where);
         $this->db->delete('device');
     }
            
     //查询获取单条
     function get_ones($where,$select='*'){
        $this->db->select($select);
        if($where){
            $this->db->where($where);
        }
        $this->db->limit(1);
        $query = $this->db->get('device');
        return $query->row_array();
    }
     //正序 全查         
     function select($where='',$order_by='',$limit='',$offset='')
     {  
         // where 条件
         if($where){
          $this->db->where($where);  
         }
         
         // order_by 条件
         if($order_by){
          $this->db->order_by($order_by);
         }
         // limit 条件
         if($offset&&$limit){
            $this->db->limit($limit,$offset);
         }
         if(!$offset&&$limit){
          $this->db->limit($limit);
         }
         
         $this->db->select('*');
         $data = $this->db->get('device');
         return  $data->result_array();
     }

     //获取设备数量
    function getDevicecount($where){
        if($where){
          $this->db->where($where);
         }
        $this->db->select('*');
        $count = $this->db->get('device');
        $count = $count->result_array();
        $device['count'] =count($count);
        $where['jihuo']=1;
        $this->db->where($where);
        $this->db->select('*');
        $jihuo = $this->db->get('device');
        $jihuo = $jihuo->result_array();
        $device['jihuo'] = count($jihuo);
        return $device;
     }

    //获取未分配设备数量
    function getDeviceweifenpei($where){
        //$where['agent2']=null;
        $where['jihuo']=0;
        $this->db->where($where);
        $this->db->select('*');
        $weifenpei = $this->db->get('device');
        $weifenpei = $weifenpei->result_array();
        $device = count($weifenpei);
        return $device;
     }

    //获取已分配设备数量
    function getDevicefenpei($where){
        //$where['agent2 <>']=null;
        $where['jihuo']=1;
        $this->db->where($where);
        $this->db->select('*');
        $fenpei = $this->db->get('device');
        $fenpei = $fenpei->result_array();
        $device = count($fenpei);
        return $device;
     }

    function getDeviceid($where){
        if($where){
          $this->db->where($where);
         }
        $this->db->select('device_sn');
        $device_id = $this->db->get('device');
        $device_id = $device_id->result_array();
        return $device_id;
     }

     function getList($where,$page,$limit,$whereor=''){
        // where 条件
         if($where){
            $this->db->where($where);  
         }
         if($whereor){
            $this->db->or_where($whereor);
         }
        $this->db->order_by('agent2 desc');
        $offset=($page-1)*$limit;
        $this->db->limit($limit,$offset);
        $data = $this->db->get('device');
        $data = $data->result_array();
        return $data;
     }
     
 }
 ?>